import {
  getHospitalListByServiceId
} from "../../assets/js/hospital-list.js";
import {
  date2YYYYMMDD,
  date2day
} from "../../utils/util.js"

Page({
  data: {
    noList: {
      img: "/assets/img/zwjl.png",
      text: "暂无可选医院"
    },
    title: "",
    serviceId: 1, //服务唯一标识的id
    hospitalList: [
      // {
      //   hospitalId: "1",
      //   img: "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=156585113304d5&di=c8607ffc4906f6b9cca1eb5ca8ee2f08&imgtype=0&src=http%3A%2F%2Fb.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2F908fa0ec08fa513db777cf78376d55fbb3fbd9b3.jpg",
      //   title: "某某某医院",
      //   time: "2019/03/02",
      //   address: "什么什么地方"
      // },
      // {
      //   hospitalId: "2",
      //   img: "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1565851133045&di=c8607ffc4906f6b9cca1eb5ca8ee2f08&imgtype=0&src=http%3A%2F%2Fb.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2F908fa0ec08fa513db777cf78376d55fbb3fbd9b3.jpg",
      //   title: "某某某医院",
      //   time: "2019/03/02",
      //   address: "什么什么地方"
      // }
    ]
  },
  onLoad: function(options) {
    let title = options.title; //头部标题
    let serviceId = options.serviceId; //服务的唯一标识id
    wx.setNavigationBarTitle({
      title: title,
    })
    this.setData({
      serviceId: serviceId,
      title: title
    })
    this.getHospitalList();
  },
  onReady: function() {},
  onShow: function() {},
  // 根据服务id获取医院列表
  getHospitalList: function() {
    let serviceId = this.data.serviceId;
    getHospitalListByServiceId({
      page: 1,
      pageSize: 1000,
      serviceId: this.data.serviceId // 领翼服务Id
    }).then(res => {
      let hospitalList = [];
      if (res.records.length) {
        res.records.forEach((item, index) => {
          let obj = item;
          obj.id = item.hospitalId; // 每家医院的唯一标识
          obj.img = item.logo || "/assets/img/authorization.png"; // 每家医院的图片
          obj.title = item.name; // 每家医院的名字
          obj.address = `${item.province}${item.city}${item.county}${item.address}` || "服务地址暂不存在"; // 每家医院的地理位置
          obj.time = `预约时间：${date2YYYYMMDD(item.serviceScheduling.schedulingDate).type2} ${date2day(item.serviceScheduling.schedulingDate)}`;
          obj.serviceId = item.hospitalServiceItemId;
          hospitalList.push(obj);
        });
      } else {
        hospitalList = [];
      }
      this.setData({
        hospitalList
      })
    })
  },
  // 点击其中一家医院做的事
  handleClickOne: function(e) {
    let detail = e.detail;
    let item = detail.item;
    let index = detail.index;
    console.log(item);
    // console.log(this.data.title)
    // 跳转详情
    wx.navigateTo({
      url: `/pages/u_detail/u_detail?serviceId=${item.serviceId}&hospitalId=${item.hospitalId}&title=${this.data.title}` // 调接口时进行修改
    })
  }
})